package backend.project.gym_management.controller;

import backend.project.gym_management.base.response.JsonResult;
import backend.project.gym_management.pojo.entity.EAvatar;
import backend.project.gym_management.service.UploadFileService;
import backend.project.gym_management.pojo.dto.CoachAddCourseParam;
import backend.project.gym_management.pojo.dto.CoachEditCourseIntroductionDTO;
import backend.project.gym_management.pojo.dto.CoachIntroductionDTO;
import backend.project.gym_management.pojo.entity.CoachIntroduction;
import backend.project.gym_management.pojo.entity.Course;
import backend.project.gym_management.pojo.entity.CourseIntroduction;
import backend.project.gym_management.service.CoachService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/v1/coach/")
@Api(tags = "04.教练功能模块")
public class CoachController {
    @Autowired
    private CoachService coachService;

    @Autowired
    private UploadFileService uploadFileService;

    @GetMapping("course/list")
    @ApiOperation("教练查看所有课程功能")
    @ApiOperationSupport(order = 10)
    public JsonResult listCourse(Long eid){
        var data = coachService.listCourse(eid);
        return JsonResult.ok(data);
    }

    @PostMapping("course/add")
    @ApiOperation("教练插入课程功能")
    @ApiOperationSupport(order = 20)
    public JsonResult addCourse(@RequestBody CoachAddCourseParam cacp) throws Exception {
        Course course = new Course();
        BeanUtils.copyProperties(cacp, course);
        course.setCount(0);
        coachService.addCourse(course);
        var datas = coachService.listCourse(cacp.getEid());
        var data = datas.get(datas.size()-1).getCid();
        return JsonResult.ok(data);
    }

    @GetMapping("course/findintro")
    @ApiOperation("教练查看描述功能")
    @ApiOperationSupport(order = 30)
    public JsonResult findCourseIntroduction(Long cid){
        var data = coachService.selectCourseIntroduction(cid);
        log.debug(data.toString());
        return JsonResult.ok(data);
    }

    @PutMapping("course/editintro")
    @ApiOperation("教练编辑描述功能")
    @ApiOperationSupport(order = 40)
    public JsonResult editCourseIntroduction(@RequestBody CoachEditCourseIntroductionDTO cei) {
        CourseIntroduction courseIntroduction = new CourseIntroduction();
        BeanUtils.copyProperties(cei, courseIntroduction);
        if (coachService.selectCourseIntroduction(courseIntroduction.getCid()) == null) {
            coachService.addCourseIntroduction(courseIntroduction);
        }else {
            coachService.updateCourseIntroduction(courseIntroduction);
        }
        return JsonResult.ok();
    }
    @GetMapping("enrollment/list")
    @ApiOperation("教练查看学员功能")
    @ApiOperationSupport(order = 50)
    public JsonResult listEnrollment(Long cid) throws Exception {
        var data = coachService.listEnrollmentVO(cid);
        return JsonResult.ok(data);
    }

    @GetMapping("enrollment/health")
    @ApiOperation("教练查看学员健康功能")
    @ApiOperationSupport(order = 60)
    public JsonResult userHealth(Long uid) throws Exception {
        var data = coachService.selectUserHealthByUid(uid);
        return JsonResult.ok(data);
    }

    @GetMapping("/intro/view")
    @ApiOperation("教练查看简历功能")
    @ApiOperationSupport(order = 70)
    public JsonResult viewCoachIntroduction(Long eid) throws Exception {
        var data = coachService.selectCoachIntroductionByEid(eid);
        return JsonResult.ok(data);
    }

    @PostMapping("/intro/edit")
    @ApiOperation("教练编辑简历功能")
    @ApiOperationSupport(order = 80)
    public JsonResult editCoachIntroduction(@RequestBody CoachIntroductionDTO cid) throws Exception {
        CoachIntroduction ci = new CoachIntroduction();
        BeanUtils.copyProperties(cid, ci);
        if(coachService.selectCoachIntroductionByEid(ci.getEid()) == null){
            coachService.addCoachIntroduction(ci);
        }else{
            coachService.updateCoachIntroduction(ci);
        }
        return JsonResult.ok();
    }

    @GetMapping("/loadAvatar")
    @ApiOperation("获取教练头像")
    @ApiOperationSupport(order = 90)
    public JsonResult loadAvatar(Long eid) throws Exception {
        EAvatar eAvatar = uploadFileService.selectEAvatar(eid);
        return JsonResult.ok(eAvatar.getAvatarPath());
    }
}
